import {useCounterStore} from "@/store/counter";
import type {App} from "vue";

const hasPermi = {
  mounted(el: any, binding: any, VNode: any): void {
    const store = useCounterStore() as any;
    const {value} = binding
    const all_permission = "*:*:*";
    const permissions = store.permissions
    if (value && value instanceof Array && value.length > 0) {
      const permissionFlag = value

      const hasPermissions = permissions.some((permission: string) => {
        return all_permission === permission || permissionFlag.includes(permission)
      })

      if (!hasPermissions) {
        VNode.ctx.props.disabled = true //指令默认开启element plus的el-button disabled属性
        // el.parentNode && el.parentNode.removeChild(el) //如果想移除当前button请解注这段代码
      }
    } else {
      throw new Error(`请设置操作权限标签值`)
    }
  }
}

export default {
  install(app: App<Element>) {
    app.directive('hasPermi', hasPermi)
  }
}
